package com.smfx.yxt.controller.sys;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.smfx.yxt.common.enums.ExcelTemplate;
import com.smfx.yxt.controller.BaseController;
import com.smfx.yxt.excel.JxlsExcelView;
import com.smfx.yxt.model.Grid;
import com.smfx.yxt.page.OrderBy;
import com.smfx.yxt.page.Page;
import com.smfx.yxt.reqVo.PointsReqVO;
import com.smfx.yxt.service.IClassPointService;
import com.smfx.yxt.service.IStudentPointService;
import com.smfx.yxt.service.ITeacherPointService;
import com.smfx.yxt.view.PointItemView;
import com.smfx.yxt.view.PointTotalView;

@RequestMapping("sys/point/report")
@Controller
public class PointReportController extends BaseController {

	private Logger logger = LoggerFactory.getLogger(PointReportController.class);
	
	@Autowired
	private ITeacherPointService teacherPointService;
	@Autowired
	private IClassPointService classPointService;
	@Autowired
	private IStudentPointService studentPointService;
	
	@RequestMapping("teacher")
	public String teacherPoint() {
		return "sys/pointReport/teacherPoint";
	}
	
	@RequestMapping("teacher/item")
	public String teacherPointItem() {
		return "sys/pointReport/teacherPointItem";
	}
	
	@RequestMapping("clazz")
	public String clazzPoint() {
		return "sys/pointReport/clazzPoint";
	}
	
	@RequestMapping("clazz/item")
	public String clazzPointItem() {
		return "sys/pointReport/clazzPointItem";
	}
	
	@RequestMapping("student")
	public String studentPoint() {
		return "sys/pointReport/studentPoint";
	}
	
	@RequestMapping("student/item")
	public String studentPointItem() {
		return "sys/pointReport/studentPointItem";
	}
	
	@RequestMapping("teacher/totalPoint")
	@ResponseBody Object teacherTotalPoint(PointsReqVO vo) {
		try {
			if(StringUtils.isNotBlank(vo.getMonthDate())) {
				vo.setMonthDate(vo.getMonthDate().replace("-", ""));
			}
			Page<PointTotalView> page = new Page<PointTotalView>();
			Grid grid = new Grid();
			List<OrderBy> sorts = new ArrayList<>();
			sorts.add(new OrderBy("grade", "desc"));
			page.setSorts(sorts);
			page.setPageNo(vo.getPage());
			page.setPageSize(vo.getRows());
			page.setParams(vo);
			List<PointTotalView> list = teacherPointService.selectTotalPointForTeacherByPage(page);
			grid.setRows(list);
			grid.setTotal(page.getTotalRecord());
			return grid;
		} catch (Exception e) {
			logger.error("---teacherTotalPoint error={}", e);
		}
		return null;
	}
	
	@RequestMapping("teacher/itemList")
	@ResponseBody Object teacherItemList(PointsReqVO vo) {
		try {
			if(StringUtils.isNotBlank(vo.getMonthDate())) {
				vo.setMonthDate(vo.getMonthDate().replace("-", ""));
			}
			Page<PointItemView> page = new Page<PointItemView>();
			Grid grid = new Grid();
			List<OrderBy> sorts = new ArrayList<>();
			sorts.add(new OrderBy("createTime", "desc"));
			page.setSorts(sorts);
			page.setPageNo(vo.getPage());
			page.setPageSize(vo.getRows());
			page.setParams(vo);
			List<PointItemView> list = teacherPointService.selectPointItemForTeacherByPage(page);
			grid.setRows(list);
			grid.setTotal(page.getTotalRecord());
			return grid;
		} catch (Exception e) {
			logger.error("---teacherItemList error={}", e);
		}
		return null;
	}
	
	@RequestMapping("teacher/totalPoint/export")
	ModelAndView teacherTotalPointExport(ModelAndView modelAndView, PointsReqVO vo) {
		try {
			if(StringUtils.isNotBlank(vo.getMonthDate())) {
				vo.setMonthDate(vo.getMonthDate().replace("-", ""));
			}
			Page<PointTotalView> page = new Page<PointTotalView>();
			List<OrderBy> sorts = new ArrayList<>();
			sorts.add(new OrderBy("grade", "desc"));
			page.setSorts(sorts);
			page.setPageSize(Integer.MAX_VALUE -1);
			page.setParams(vo);
			List<PointTotalView> list = teacherPointService.selectTotalPointForTeacherByPage(page);
			
			JxlsExcelView view = new JxlsExcelView(ExcelTemplate.POINT_TOTAL_TEACHER);
			modelAndView.setView(view);
			modelAndView.getModelMap().put("list", list);
		} catch (Exception e) {
			logger.error("--teacherTotalPointExport error ",e);
		}
		return modelAndView;
	}
	
	@RequestMapping("teacher/itemList/export")
	ModelAndView teacherItemListExport(ModelAndView modelAndView, PointsReqVO vo) {
		try {
			if(StringUtils.isNotBlank(vo.getMonthDate())) {
				vo.setMonthDate(vo.getMonthDate().replace("-", ""));
			}
			Page<PointItemView> page = new Page<PointItemView>();
			List<OrderBy> sorts = new ArrayList<>();
			sorts.add(new OrderBy("createTime", "desc"));
			page.setSorts(sorts);
			page.setPageSize(Integer.MAX_VALUE -1);
			page.setParams(vo);
			List<PointItemView> list = teacherPointService.selectPointItemForTeacherByPage(page);
			
			JxlsExcelView view = new JxlsExcelView(ExcelTemplate.POINT_ITEM_TEACHER);
			modelAndView.setView(view);
			modelAndView.getModelMap().put("list", list);
		} catch (Exception e) {
			logger.error("--teacherItemListExport error ",e);
		}
		return modelAndView;
	}
	
	@RequestMapping("clazz/totalPoint")
	@ResponseBody Object clazzTotalPoint(PointsReqVO vo) {
		try {
			if(StringUtils.isNotBlank(vo.getMonthDate())) {
				vo.setMonthDate(vo.getMonthDate().replace("-", ""));
			}
			Page<PointTotalView> page = new Page<PointTotalView>();
			Grid grid = new Grid();
			List<OrderBy> sorts = new ArrayList<>();
			sorts.add(new OrderBy("grade", "desc"));
			sorts.add(new OrderBy("clazz"));
			page.setSorts(sorts);
			page.setPageNo(vo.getPage());
			page.setPageSize(vo.getRows());
			page.setParams(vo);
			List<PointTotalView> list = classPointService.selectTotalPointForClassByPage(page);
			grid.setRows(list);
			grid.setTotal(page.getTotalRecord());
			return grid;
		} catch (Exception e) {
			logger.error("---clazzTotalPoint error={}", e);
		}
		return null;
	}
	
	@RequestMapping("clazz/itemList")
	@ResponseBody Object clazzitemList(PointsReqVO vo) {
		try {
			if(StringUtils.isNotBlank(vo.getMonthDate())) {
				vo.setMonthDate(vo.getMonthDate().replace("-", ""));
			}
			Page<PointItemView> page = new Page<PointItemView>();
			Grid grid = new Grid();
			List<OrderBy> sorts = new ArrayList<>();
			sorts.add(new OrderBy("createTime", "desc"));
			page.setSorts(sorts);
			page.setPageNo(vo.getPage());
			page.setPageSize(vo.getRows());
			page.setParams(vo);
			List<PointItemView> list = classPointService.selectPointItemForClassByPage(page);
			grid.setRows(list);
			grid.setTotal(page.getTotalRecord());
			return grid;
		} catch (Exception e) {
			logger.error("---clazzitemList error={}", e);
		}
		return null;
	}
	
	@RequestMapping("clazz/totalPoint/export")
	ModelAndView clazzTotalPointExport(ModelAndView modelAndView, PointsReqVO vo) {
		try {
			if(StringUtils.isNotBlank(vo.getMonthDate())) {
				vo.setMonthDate(vo.getMonthDate().replace("-", ""));
			}
			Page<PointTotalView> page = new Page<PointTotalView>();
			List<OrderBy> sorts = new ArrayList<>();
			sorts.add(new OrderBy("grade", "desc"));
			sorts.add(new OrderBy("clazz"));
			page.setSorts(sorts);
			page.setPageSize(Integer.MAX_VALUE -1);
			page.setParams(vo);
			List<PointTotalView> list = classPointService.selectTotalPointForClassByPage(page);
			
			JxlsExcelView view = new JxlsExcelView(ExcelTemplate.POINT_TOTAL_CLAZZ);
			modelAndView.setView(view);
			modelAndView.getModelMap().put("list", list);
		} catch (Exception e) {
			logger.error("--teacherTotalPointExport error ",e);
		}
		return modelAndView;
	}
	
	@RequestMapping("clazz/itemList/export")
	ModelAndView clazzitemListExport(ModelAndView modelAndView, PointsReqVO vo) {
		try {
			if(StringUtils.isNotBlank(vo.getMonthDate())) {
				vo.setMonthDate(vo.getMonthDate().replace("-", ""));
			}
			Page<PointItemView> page = new Page<PointItemView>();
			List<OrderBy> sorts = new ArrayList<>();
			sorts.add(new OrderBy("createTime", "desc"));
			page.setSorts(sorts);
			page.setPageSize(Integer.MAX_VALUE -1);
			page.setParams(vo);
			List<PointItemView> list = classPointService.selectPointItemForClassByPage(page);
			
			JxlsExcelView view = new JxlsExcelView(ExcelTemplate.POINT_ITEM_CLAZZ);
			modelAndView.setView(view);
			modelAndView.getModelMap().put("list", list);
		} catch (Exception e) {
			logger.error("--clazzitemListExport error ",e);
		}
		return modelAndView;
	}
	
	@RequestMapping("student/totalPoint")
	@ResponseBody Object studentTotalPoint(PointsReqVO vo) {
		try {
			if(StringUtils.isNotBlank(vo.getMonthDate())) {
				vo.setMonthDate(vo.getMonthDate().replace("-", ""));
			}
			Page<PointTotalView> page = new Page<PointTotalView>();
			Grid grid = new Grid();
			List<OrderBy> sorts = new ArrayList<>();
			sorts.add(new OrderBy("grade", "desc"));
			sorts.add(new OrderBy("clazz"));
			page.setSorts(sorts);
			page.setPageNo(vo.getPage());
			page.setPageSize(vo.getRows());
			page.setParams(vo);
			List<PointTotalView> list = studentPointService.selectTotalPointForStudentByPage(page);
			grid.setRows(list);
			grid.setTotal(page.getTotalRecord());
			return grid;
		} catch (Exception e) {
			logger.error("---teacherTotalPoint error={}", e);
		}
		return null;
	}
	
	@RequestMapping("student/itemList")
	@ResponseBody Object studentItemList(PointsReqVO vo) {
		try {
			if(StringUtils.isNotBlank(vo.getMonthDate())) {
				vo.setMonthDate(vo.getMonthDate().replace("-", ""));
			}
			Page<PointItemView> page = new Page<PointItemView>();
			Grid grid = new Grid();
			List<OrderBy> sorts = new ArrayList<>();
			sorts.add(new OrderBy("grade", "desc"));
			sorts.add(new OrderBy("createTime", "desc"));
			page.setSorts(sorts);
			page.setPageNo(vo.getPage());
			page.setPageSize(vo.getRows());
			page.setParams(vo);
			List<PointItemView> list = studentPointService.selectPointItemForStudentByPage(page);
			grid.setRows(list);
			grid.setTotal(page.getTotalRecord());
			return grid;
		} catch (Exception e) {
			logger.error("---studentItemList error={}", e);
		}
		return null;
	}
	
	@RequestMapping("student/totalPoint/export")
	ModelAndView studentTotalPointExport(ModelAndView modelAndView, PointsReqVO vo) {
		try {
			if(StringUtils.isNotBlank(vo.getMonthDate())) {
				vo.setMonthDate(vo.getMonthDate().replace("-", ""));
			}
			Page<PointTotalView> page = new Page<PointTotalView>();
			List<OrderBy> sorts = new ArrayList<>();
			sorts.add(new OrderBy("grade", "desc"));
			sorts.add(new OrderBy("clazz"));
			page.setSorts(sorts);
			page.setPageSize(Integer.MAX_VALUE -1);
			page.setParams(vo);
			List<PointTotalView> list = studentPointService.selectTotalPointForStudentByPage(page);
			
			JxlsExcelView view = new JxlsExcelView(ExcelTemplate.POINT_TOTAL_STUDENT);
			modelAndView.setView(view);
			modelAndView.getModelMap().put("list", list);
		} catch (Exception e) {
			logger.error("--studentTotalPointExport error ",e);
		}
		return modelAndView;
	}
	
	@RequestMapping("student/itemList/export")
	ModelAndView studentItemListExport(ModelAndView modelAndView, PointsReqVO vo) {
		try {
			if(StringUtils.isNotBlank(vo.getMonthDate())) {
				vo.setMonthDate(vo.getMonthDate().replace("-", ""));
			}
			Page<PointItemView> page = new Page<PointItemView>();
			List<OrderBy> sorts = new ArrayList<>();
			sorts.add(new OrderBy("grade", "desc"));
			sorts.add(new OrderBy("createTime", "desc"));
			page.setSorts(sorts);
			page.setPageSize(Integer.MAX_VALUE -1);
			page.setParams(vo);
			List<PointItemView> list = studentPointService.selectPointItemForStudentByPage(page);
			
			JxlsExcelView view = new JxlsExcelView(ExcelTemplate.POINT_ITEM_STUDENT);
			modelAndView.setView(view);
			modelAndView.getModelMap().put("list", list);
		} catch (Exception e) {
			logger.error("--studentItemListExport error ",e);
		}
		return modelAndView;
	}
}
